/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package businesscard.DAO;

import bc.entity.Bophan;
import bc.entity.Capbac;
import bc.entity.Congty;
import bc.entity.Lienhe;
import bc.entity.Quanhuyen;
import bc.entity.Thanhpho;
import bc.util.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author THICH HOA DONG TIEN
 */
public class SearchDAO {

    private Session session;

    public SearchDAO() {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        // this.session = factory.getCurrentSession();
        this.session = factory.openSession();
    }

    public List<Lienhe> Search(String name) {
        session.beginTransaction().begin();
        LienheDAO lhdao1 = new LienheDAO();
        List<Lienhe> lhList1 = lhdao1.getLienheLikeName(name);
        if (lhList1.isEmpty()) {
            CongtyDAO ctdao1 = new CongtyDAO();
            List<Congty> ctList1 = ctdao1.getCongtyLikesName(name);
            if (!ctList1.isEmpty()) {
                for (int i = 0; i < ctList1.size(); i++) {
                    LienheDAO lhdao2 = new LienheDAO();
                    List<Lienhe> lhList2 = lhdao2.getLienheByCongty(ctList1.get(i));
                    for (int j = 0; j < lhList2.size(); j++) {
                        lhList1.add(lhList2.get(j));
                    }
                }
                return lhList1;
            } else {
                CapbacDAO cbdao1 = new CapbacDAO();
                List<Capbac> cbList1 = cbdao1.getCapbacLikeName(name);
                if (!cbList1.isEmpty()) {
                    for (int k = 0; k < cbList1.size(); k++) {
                        LienheDAO lhdao3 = new LienheDAO();
                        List<Lienhe> lhList3 = lhdao3.getLienheByCapbac(cbList1.get(k));
                        for (int h = 0; h < lhList3.size(); h++) {
                            lhList1.add(lhList3.get(h));
                        }
                    }
                    return lhList1;

                } else {
                    BophanDAO bpdao1 = new BophanDAO();
                    List<Bophan> bpList1 = bpdao1.getBophanLikeName(name);
                    if (!bpList1.isEmpty()) {
                        for (int x = 0; x < bpList1.size(); x++) {
                            LienheDAO lhdao4 = new LienheDAO();
                            List<Lienhe> lhList4 = lhdao4.getLienheByBophan(bpList1.get(x));
                            for (int y = 0; x < lhList4.size(); y++) {
                                lhList1.add(lhList4.get(y));
                            }

                        }
                        return lhList1;
                    } else {
                        QuanhuyenDAO qhdao1 = new QuanhuyenDAO();
                        List<Quanhuyen> qhList1 = qhdao1.getQuanhuyenLikeName(name);
                        if (!qhList1.isEmpty()) {
                            for (int i = 0; i < qhList1.size(); i++) {
                                LienheDAO lhdao5 = new LienheDAO();
                                List<Lienhe> lhList5 = lhdao5.getLienheByQuanhuyen(qhList1.get(i));
                                for (int j = 0; j < lhList5.size(); j++) {
                                    lhList1.add(lhList5.get(j));
                                }
                            }
                            return lhList1;
                        } else {
                            // đến chỗ này
                            List<Quanhuyen> qhList = new ArrayList<Quanhuyen>();
                            ThanhphoDAO tpdao1 = new ThanhphoDAO();
                            List<Thanhpho> tpList1 = tpdao1.getThanhphoLikeName(name);
                            if (!tpList1.isEmpty()) {
                                for (int i = 0; i < tpList1.size(); i++) {
                                    QuanhuyenDAO qhdao2 = new QuanhuyenDAO();
                                    List<Quanhuyen> qhList2 = qhdao2.getQuanhuyenByTP(tpList1.get(i));
                                    for (int j = 0; j < qhList2.size(); j++) {
                                        qhList.add(qhList2.get(j));
                                    }

                                }
                                if (!qhList.isEmpty()) {
                                    for (int k = 0; k < qhList.size(); k++) {
                                        LienheDAO lhdao6 = new LienheDAO();
                                        List<Lienhe> lhList6 = lhdao6.getLienheByQuanhuyen(qhList.get(k));
                                        for (int h = 0; h < lhList6.size(); h++) {
                                            lhList1.add(lhList6.get(h));
                                        }
                                    }
                                    return lhList1;
                                } else {
                                    return null;
                                }
                            }
                        }
                    }

                }

            }

        } else {
            return lhList1;
        }

        return null;

    }
   
            
     public static void main(String[] args) {
         SearchDAO tim=new SearchDAO();
         String name="giao su";
         List<Lienhe> lhlist= tim.Search(name);
         if(lhlist.isEmpty()){
              System.out.println("khong co lien he nao");
         }
         for(int i=0; i<lhlist.size();i++){
             System.out.println(lhlist.get(i).getTen());
         }
     }
}
